import com.sun.xml.internal.ws.addressing.WsaActionUtil;

public class Test {
    //字符串修改
    public static void main(String[] args) {
        StringBuilder stringBuilder = new StringBuilder("hello");
        stringBuilder.append("world");
        System.out.println(stringBuilder);
        //StringBuilder的对象是可变的
    }
    public static void main7(String[] args) {
        StringBuilder stringBuilder = new StringBuilder("hello"); //比String多了很多方法
        //StringBuilder stringBuilder = "hello"; //erro 不能直接赋值
        String str = stringBuilder.toString();
        System.out.println(str);
    }
    public static void main6(String[] args) {
        String str = "abcd";
        for(int i = 0; i < 100; i++) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(str);
            stringBuilder.append(i);
            str = stringBuilder.toString();
        }
        System.out.println(str);
    }
    public static void main5(String[] args) {
        String str = "abcd";
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(str);
        for(int i = 0; i < 100; i++) {
            stringBuilder.append(i);
        }
        System.out.println(stringBuilder);
    }
    public static void main4(String[] args) {
        String str = "abcd";
        for(int i = 0; i < 100; i++) { //每次循环都new一个对象
            str += i;
        }
        System.out.println(str);
    }
    public static void main3(String[] args) {
        final int[] array = {1, 2, 3, 4}; //final修饰 则指向不可变 仍然可以更改值
        //array = new int[10]; //erro
        array[0] = 0;

        //字符串对象在创建好之后不能再被修改 因为value数组被private修饰
        String s = "hello";
        s = "world";//只是修改了引用指向的对象，而非修改对象内容
    }
    public static void main2(String[] args) {
        char[] chars = {'a', 'b', 'c'};
        String s1 = new String(chars);
        s1.intern(); //把s1指向的对象入池 （必须是常量池不存在这个对象的时候）"abc"不存在于字符串常量池
        String s2 = "abc";
        System.out.println(s1 == s2);
    }
    public static void main1(String[] args) { //字符串常量池
        String s1 = "abcd";
        String s2 = "abcd";
        String s3 = new String("abcd");
        String s4 = new String("abcd");

        System.out.println(s1 == s2);
        System.out.println(s3 == s4);
        System.out.println(s1 == s3);
    }
}
